<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/06/02
// +----------------------------------------------------------------------

namespace app\erp\model;

use think\Db;
use think\Model;

/**
 * 退换货管理
 * @package app\erp\model
 */
class ErpReturnandexchange extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_erp_goods_returnandexchange';

    public function getList($map)
    {
        $field = 'r.*,rs.sku_id,rs.num,os.price';
        $res = Db::table('xm_erp_goods_returnandexchange')
            ->alias('r')
            ->leftJoin('erp_goods_returnandexchange_sku rs','r.id = rs.rc_id')
            ->leftJoin('xm_erp_goods_order_skulist os','rs.sku_id = os.sku_id AND os.order_id = r.order_id')
            ->where($map)
            ->field($field)
            ->order('r.create_time desc')
            ->paginate();
        if($res){
            return array_out(200, 'success',$res);
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 添加数据
     * @param order_id 	                int     订单id
     * @param type 		                int     类型 1退货 2换货
     * @param reason 		            string  退货理由
     * @param content 		            text    问题描述
     * @param img_url1 	                string  图片凭证1
     * @param img_url2 	                string  图片凭证2
     * @param img_url3 	                string  图片凭证3
     * @param create_time 		        int     创建时间
     * @param update_time 		        int     编辑时间
     * @param uid 		                int     添加人
     * @param return_order_code 		int     退换货订单号
     * @param return_status 		    int     订单状态 0申请中 1已撤回 2已完成
     * @param sku_id 		            int     规格id
     * @param num 		                int     数量
     * @param create_time 		        int     创建时间
     * @param update_time 		        int     编辑时间

     * @author dou<51779987@qq.com>
     * @return array_out(code, message, (int)id)
     */
    public function addData($user_id, $order_id, $type, $reason, $content, $img_url1, $img_url2, $img_url3, $return_order_code, $return_status,$sku_id,$num){
        $data['order_id']           = $order_id;
        $data['type']               = $type;
        $data['reason']             = $reason;
        $data['content']            = $content;
        $data['img_url1']           = $img_url1;
        $data['img_url2']           = $img_url2;
        $data['img_url3']           = $img_url3;
        $data['uid']                = $user_id;
        $data['return_order_code']  = $return_order_code;
        $data['return_status']      = $return_status;
        $data['create_time']        = time();
        $data['update_time']        = time();
        $res = $this -> isUpdate(false) ->data($data, true) -> save();
        if($res){
            $data_sku = [];
            $data_sku['rc_id']      = $this->id;
            $data_sku['sku_id']     = $sku_id;
            $data_sku['num']        = $num;
            $data_sku['create_time']= time();
            $data_sku['update_time']= time();
            $res_sku = Db::name('erp_goods_returnandexchange_sku')->insert($data_sku);
        }
        if($res_sku){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }


    /**
     * 编辑数据
     * @param id 	                    int     申请id
     * @param order_id 	                int     订单id
     * @param type 		                int     类型 1退货 2换货
     * @param reason 		            string  退货理由
     * @param content 		            text    问题描述
     * @param img_url1 	                string  图片凭证1
     * @param img_url2 	                string  图片凭证2
     * @param img_url3 	                string  图片凭证3
     * @param create_time 		        int     创建时间
     * @param update_time 		        int     编辑时间
     * @param uid 		                int     添加人
     * @param return_order_code 		int     退换货订单号
     * @param return_status 		    int     订单状态 0申请中 1已撤回 2已完成
     * @param sku_id 		            int     规格id
     * @param num 		                int     数量
     * @param create_time 		        int     创建时间
     * @param update_time 		        int     编辑时间

     * @author dou<51779987@qq.com>
     * @return array_out(code, message, (int)id)
     */
    public function editData($user_id,$id , $order_id, $type, $reason, $content, $img_url1, $img_url2, $img_url3, $return_order_code, $return_status,$sku_id,$num){
        $map = [];
        $map['id'] = $id;

        $data = [];
        $data['order_id']           = $order_id;
        $data['type']               = $type;
        $data['reason']             = $reason;
        $data['content']            = $content;
        $data['img_url1']           = $img_url1;
        $data['img_url2']           = $img_url2;
        $data['img_url3']           = $img_url3;
        $data['uid']                = $user_id;
        $data['return_order_code']  = $return_order_code;
        $data['return_status']      = $return_status;
        $data['update_time']        = time();

        $res = $this ->update($data, $map);
        if($res){
            $data_sku = [];
            $data_sku['sku_id']     = $sku_id;
            $data_sku['num']        = $num;
            $data_sku['update_time']= time();
            $res_sku = Db::name('erp_goods_returnandexchange_sku') ->where('rc_id', $id)->update($data_sku);
        }
        if($res_sku){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }


    /**
     * 更改状态
     * @param $user_id
     * @param $id
     * @param $return_status
     * @author dou<51779987@qq.com>
     * @return array_out(code, message, (int)id)
     */
    public function editStatus($user_id,$id,$return_status ){
        $map = [];
        $map['id'] = $id;

        $data = [];
        $data['return_status']      = $return_status;
        $data['update_time']        = time();

        $res = $this ->update($data, $map);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }

	
	/**
     * 删除数据
     * @param int $id
     * @author dou<51779987@qq.com>
     * @return bool
     */
    public function deleteData($id){
        $res = self::destroy($id);
        if($res){
            $res_sku = Db::table('erp_goods_returnandexchange_sku')->where('rc_id',$id)->delete();
        }
        if($res_sku){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }
}
